{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Bulls and Cows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: getHint"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #猜数字游戏"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你在和朋友一起玩 <a href=\"https://baike.baidu.com/item/%E7%8C%9C%E6%95%B0%E5%AD%97/83200?fromtitle=Bulls+and+Cows&amp;fromid=12003488&amp;fr=aladdin\" target=\"_blank\">猜数字（Bulls and Cows）</a>游戏，该游戏规则如下：</p>\n",
    "\n",
    "<p>写出一个秘密数字，并请朋友猜这个数字是多少。朋友每猜测一次，你就会给他一个包含下述信息的提示：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>猜测数字中有多少位属于数字和确切位置都猜对了（称为 \"Bulls\"，公牛），</li>\n",
    "\t<li>有多少位属于数字猜对了但是位置不对（称为 \"Cows\"，奶牛）。也就是说，这次猜测中有多少位非公牛数字可以通过重新排列转换成公牛数字。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个秘密数字&nbsp;<code>secret</code> 和朋友猜测的数字&nbsp;<code>guess</code> ，请你返回对朋友这次猜测的提示。</p>\n",
    "\n",
    "<p>提示的格式为 <code>\"xAyB\"</code> ，<code>x</code> 是公牛个数， <code>y</code> 是奶牛个数，<code>A</code> 表示公牛，<code>B</code>&nbsp;表示奶牛。</p>\n",
    "\n",
    "<p>请注意秘密数字和朋友猜测的数字都可能含有重复数字。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>secret = \"1807\", guess = \"7810\"\n",
    "<strong>输出：</strong>\"1A3B\"\n",
    "<strong>解释：</strong>数字和位置都对（公牛）用 '|' 连接，数字猜对位置不对（奶牛）的采用斜体加粗标识。\n",
    "\"1807\"\n",
    "  |\n",
    "\"<em><strong>7</strong></em>8<em><strong>10</strong></em>\"</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>secret = \"1123\", guess = \"0111\"\n",
    "<strong>输出：</strong>\"1A1B\"\n",
    "<strong>解释：</strong>数字和位置都对（公牛）用 '|' 连接，数字猜对位置不对（奶牛）的采用斜体加粗标识。\n",
    "\"1123\"        \"1123\"\n",
    "  |      or     |\n",
    "\"01<em><strong>1</strong></em>1\"        \"011<em><strong>1</strong></em>\"\n",
    "注意，两个不匹配的 1 中，只有一个会算作奶牛（数字猜对位置不对）。通过重新排列非公牛数字，其中仅有一个 1 可以成为公牛数字。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= secret.length, guess.length &lt;= 1000</code></li>\n",
    "\t<li><code>secret.length == guess.length</code></li>\n",
    "\t<li><code>secret</code> 和 <code>guess</code> 仅由数字组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [bulls-and-cows](https://leetcode.cn/problems/bulls-and-cows/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [bulls-and-cows](https://leetcode.cn/problems/bulls-and-cows/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"1807\"\\n\"7810\"', '\"1123\"\\n\"0111\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        def lt299(secret,guess):\n",
    "            d1,d2=defaultdict(int),defaultdict(int)\n",
    "            for ch in secret:d1[ch]+=1\n",
    "            for ch in guess:d2[ch]+=1\n",
    "            bulls,cows=0,0\n",
    "            i=1\n",
    "            while i<=min(len(secret),len(guess)):\n",
    "                if secret[-i]==guess[-i]:bulls+=1\n",
    "                i+=1\n",
    "            for key in d1.keys():\n",
    "                cows+=min(d1[key],d2[key])\n",
    "            cows-=bulls\n",
    "            return str(bulls)+'A'+str(cows)+'B'\n",
    "        return lt299(secret,guess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        min_len=min(len(secret),len(guess))\n",
    "        a=0\n",
    "        index=[]\n",
    "        for i in range(min_len):\n",
    "            if secret[i]==guess[i]:\n",
    "                a+=1\n",
    "                index.append(i)#相同位置的索引\n",
    "        s_dict={}\n",
    "        for i in range(len(secret)):\n",
    "            if i not in index:\n",
    "                if secret[i] in s_dict.keys():\n",
    "                    s_dict[secret[i]]+=1\n",
    "                else:\n",
    "                    s_dict[secret[i]]=1\n",
    "        t_dict={}\n",
    "        for i in range(len(guess)):\n",
    "            if i not in index:\n",
    "                if guess[i] in t_dict.keys():\n",
    "                    t_dict[guess[i]]+=1\n",
    "                else:\n",
    "                    t_dict[guess[i]]=1\n",
    "        b=0\n",
    "        for i in s_dict.keys():\n",
    "                if i in t_dict.keys():\n",
    "                    b+=min(s_dict[i],t_dict[i])\n",
    "        return str(a)+\"A\"+str(b)+\"B\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        A, B = 0, 0\n",
    "\n",
    "        s_map, g_map = Counter(), Counter()\n",
    "\n",
    "        for s, g in zip(secret, guess):\n",
    "            if s == g:\n",
    "                A += 1\n",
    "            else:\n",
    "                s_map[s] += 1\n",
    "                g_map[g] += 1\n",
    "        \n",
    "        for num, freq in s_map.items():\n",
    "            if num in g_map:\n",
    "                B += min(s_map[num], g_map[num])\n",
    "                \n",
    "        return f\"{A}A{B}B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        sec_list = list(secret)\n",
    "        gus_list = list(guess)\n",
    "        collection_sec = collections.Counter(sec_list)\n",
    "        collection_gus = collections.Counter(gus_list)\n",
    "        count = 0\n",
    "        countA = 0\n",
    "        for i in collection_gus:\n",
    "            if i in collection_sec:\n",
    "\n",
    "                count += min(collection_gus[i], collection_sec[i])\n",
    "        for i in range(0, len(sec_list)):\n",
    "            if sec_list[i] == gus_list[i]:\n",
    "                countA += 1\n",
    "        res = str(countA) + 'A' + str(count-countA) + 'B'\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        g = {}\n",
    "        s = {}\n",
    "        a = 0\n",
    "        b = 0\n",
    "        for gu, se in zip(guess, secret):\n",
    "            if gu == se:\n",
    "                a += 1\n",
    "            else:\n",
    "                if gu not in g:\n",
    "                    g[gu] = 0\n",
    "                g[gu] += 1\n",
    "                if se not in s:\n",
    "                    s[se] = 0\n",
    "                s[se] += 1\n",
    "        for se in s:\n",
    "            if se in g:\n",
    "                b += min(s[se],g[se])\n",
    "        return str(a) + 'A' + str(b) + 'B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "import unittest\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        na, nb = 0, 0\n",
    "\n",
    "        char2count = {}            # char2count\n",
    "        for c1 in secret:\n",
    "            if c1 in char2count:\n",
    "                char2count[c1] += 1\n",
    "            else:\n",
    "                char2count[c1] = 1\n",
    "\n",
    "        for c2 in guess:\n",
    "            if c2 in char2count:\n",
    "                char2count[c2] -= 1\n",
    "                if char2count[c2] == 0:\n",
    "                    char2count.pop(c2)\n",
    "                nb += 1\n",
    "\n",
    "        for c1, c2 in zip(secret, guess):\n",
    "            if c1 == c2:\n",
    "                na += 1\n",
    "        nb -= na\n",
    "\n",
    "        ans = f\"{na}A{nb}B\"\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls = 0\n",
    "        minS, minG = [0] * 10, [0] * 10\n",
    "        for s, g in zip(secret, guess):\n",
    "            if s == g:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                minS[int(s)] += 1\n",
    "                minG[int(g)] += 1\n",
    "        \n",
    "        cows = sum([min(s, g) for s, g in zip(minS, minG)])\n",
    "        \n",
    "        return f\"{bulls}A{cows}B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls, cnts_s, cnts_g = 0, defaultdict(int), defaultdict(int)\n",
    "        for i, c in enumerate(secret):\n",
    "            if guess[i] == c:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cnts_s[c] += 1\n",
    "                cnts_g[guess[i]] += 1\n",
    "        cows = 0\n",
    "        for k in cnts_s.keys():\n",
    "            cows += min(cnts_s[k], cnts_g[k])\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str: \n",
    "        bulls,cows = 0,0\n",
    "        cnt1,cnt2 = Counter(),Counter()\n",
    "        for i,j in zip(secret,guess):\n",
    "            if i==j:\n",
    "                bulls+=1\n",
    "            else:\n",
    "                cnt1[i]+=1\n",
    "                cnt2[j]+=1\n",
    "        for j,v in cnt2.items():\n",
    "            if j in cnt1:\n",
    "                cows+=min(cnt1[j],cnt2[j])\n",
    "        return f'{bulls}A{cows}B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls, cows = 0, 0\n",
    "        for b, c in zip(secret, guess):\n",
    "            if b == c:\n",
    "                bulls += 1\n",
    "        s_c, g_c = dict(collections.Counter(secret)), dict(collections.Counter(guess))\n",
    "        for k, v in s_c.items():\n",
    "            cows += min(v, g_c.get(k, 0))\n",
    "        return str(bulls) + 'A' + str(cows-bulls) + 'B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        sec_list = list(secret)\n",
    "        gus_list = list(guess)\n",
    "        collection_sec = collections.Counter(sec_list)\n",
    "        collection_gus = collections.Counter(gus_list)\n",
    "        count = 0\n",
    "        countA = 0\n",
    "        for i in collection_gus:\n",
    "            if i in collection_sec:\n",
    "\n",
    "                count += min(collection_gus[i], collection_sec[i])\n",
    "        for i in range(0, len(sec_list)):\n",
    "            if sec_list[i] == gus_list[i]:\n",
    "                countA += 1\n",
    "        res = str(countA) + 'A' + str(count-countA) + 'B'\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "\n",
    "        bull = sum(secret[i] == guess[i] for i in range(len(secret)))\n",
    "        cow = sum((Counter(secret) & Counter(guess)).values()) - bull\n",
    "\n",
    "        return f'{bull}A{cow}B'\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        ls = len(secret)\n",
    "        count1 = 0\n",
    "        count2 = 0\n",
    "        ct1 = collections.defaultdict(int)\n",
    "        ct2 = collections.defaultdict(int)\n",
    "        for char1,char2 in zip(list(secret),list(guess)):\n",
    "            if char1 ==char2:\n",
    "                count1+=1\n",
    "            else:\n",
    "                ct1[char1]+=1\n",
    "                ct2[char2]+=1\n",
    "        for key,value in ct2.items():\n",
    "            count2+=min(value,ct1[key])\n",
    "        return str(count1)+\"A\"+str(count2)+\"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "         # 进行数字和位的比对\n",
    "        n = len(secret)\n",
    "        ans = 0\n",
    "        dicts_secret = collections.defaultdict(int)\n",
    "        ansd = []\n",
    "        for i in range(n):\n",
    "            dicts_secret[secret[i]]+=1\n",
    "            if secret[i] == guess[i]:\n",
    "                ans+=1\n",
    "                dicts_secret[secret[i]]-=1\n",
    "                ansd.append(i)\n",
    "        cur = 0\n",
    "        for i in range(n):\n",
    "            if guess[i] in dicts_secret and dicts_secret[guess[i]]!=0 and i not in ansd:\n",
    "                cur+=1\n",
    "                dicts_secret[guess[i]]-=1\n",
    "        return f\"{ans}A{cur}B\"\n",
    "\n",
    "            \n",
    "            \n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        c=Counter(secret)\n",
    "        s=Counter(guess)\n",
    "        count,a=0,0\n",
    "        for k,v in c.items():\n",
    "            if s[k]==v:\n",
    "                count+=v\n",
    "            elif s[k]<v:\n",
    "                count+=s[k]\n",
    "            elif s[k]>v:\n",
    "                count+=v\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i]==guess[i]:\n",
    "                a+=1\n",
    "        return str(a)+'A'+str(count-a)+'B'\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        a=0\n",
    "        b=0\n",
    "        map={}\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i]==guess[i]:\n",
    "                a+=1\n",
    "            else:\n",
    "                if secret[i] not in map:\n",
    "                    map[secret[i]]=1\n",
    "                else:\n",
    "                    map[secret[i]]+=1\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i]!=guess[i] and guess[i] in map:\n",
    "                if map[guess[i]]>0:\n",
    "                    b+=1\n",
    "                    map[guess[i]]-=1\n",
    "        return f\"{a}A{b}B\"            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        n = len(guess)\n",
    "        A, B = 0, 0\n",
    "        record = [0] * 10\n",
    "        for i in range(n):\n",
    "            record[int(secret[i])] += 1\n",
    "            if guess[i] == secret[i]:\n",
    "                A += 1\n",
    "        for i in range(n):\n",
    "            if record[int(guess[i])] > 0:\n",
    "                B += 1\n",
    "                record[int(guess[i])] -= 1\n",
    "        return str(A) + 'A' + str(B - A) + 'B'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls, cnts_s, cnts_g = 0, defaultdict(int), defaultdict(int)\n",
    "        for i,c in enumerate(secret):\n",
    "            if guess[i] == c:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cnts_s[c] += 1\n",
    "                cnts_g[guess[i]] += 1\n",
    "        cows = 0\n",
    "        for k in cnts_s.keys():\n",
    "            cows += min(cnts_s[k], cnts_g[k])\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        a, cnts_s, cnts_g = 0, defaultdict(int), defaultdict(int)\n",
    "        for i, c in enumerate(secret):\n",
    "            if guess[i] == c:\n",
    "                a += 1\n",
    "            else:\n",
    "                cnts_s[c] += 1\n",
    "                cnts_g[guess[i]] += 1\n",
    "        b = 0\n",
    "        for k in cnts_s.keys():\n",
    "            b += min(cnts_s[k], cnts_g[k])\n",
    "        return str(a) + \"A\" + str(b) + \"B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        zero_to_nine_secret = [0] * 10\n",
    "        zero_to_nine_guess = [0] * 10\n",
    "        x, y = 0, 0\n",
    "        for secret_letter, guess_letter in zip(secret, guess):\n",
    "            if secret_letter == guess_letter:\n",
    "                x += 1\n",
    "            else:\n",
    "                zero_to_nine_secret[int(secret_letter)] += 1 \n",
    "                zero_to_nine_guess[int(guess_letter)] += 1\n",
    "        for secret_count, guess_count in zip(zero_to_nine_secret, zero_to_nine_guess):\n",
    "            y += min(secret_count, guess_count)\n",
    "        return f'{x}A{y}B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 2. 解法二: 数组 将数组当哈希表使用\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        n = len(secret)\n",
    "        bulls = 0\n",
    "        cows = 0\n",
    "        cnt_secret = [0] * 10\n",
    "        cnt_guess = [0] * 10\n",
    "        for i in range(n):\n",
    "            c1 = ord(secret[i]) - ord(\"0\")\n",
    "            c2 = ord(guess[i]) - ord(\"0\")\n",
    "            if c1 == c2:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cnt_secret[c1] += 1\n",
    "                cnt_guess[c2] += 1\n",
    "\n",
    "        for i in range(10):\n",
    "            cows += min(cnt_secret[i], cnt_guess[i])\n",
    "\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        cow = 0\n",
    "        for i,j in zip(secret, guess):\n",
    "            if i == j:\n",
    "                cow += 1\n",
    "        bull = 0\n",
    "        cnt1 = Counter(secret)\n",
    "        cnt2 = Counter(guess)\n",
    "        for key, value in cnt1.items():\n",
    "            bull += min(cnt2.get(key,0),value)\n",
    "        return f\"{cow}A{bull-cow}B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        n = len(secret)\n",
    "        A = 0\n",
    "        B = 0\n",
    "        hashsct = {}\n",
    "        hashges = {}\n",
    "        for i in range(n):\n",
    "            if hashsct.get(secret[i],None) == None:\n",
    "                hashsct[secret[i]] = 1\n",
    "            else:\n",
    "                hashsct[secret[i]] += 1\n",
    "\n",
    "            if hashges.get(guess[i],None) == None:\n",
    "                hashges[guess[i]] = 1\n",
    "            else:\n",
    "                hashges[guess[i]] += 1\n",
    "            \n",
    "            if secret[i] == guess[i]:\n",
    "                A += 1\n",
    "                hashsct[secret[i]] -= 1\n",
    "                hashges[guess[i]] -= 1\n",
    "        \n",
    "        for i,j in hashges.items():\n",
    "            if j == 0:\n",
    "                continue\n",
    "            if hashsct.get(i,None) != None and hashsct.get(i) > 0:\n",
    "                B += min(hashsct.get(i),j)\n",
    "        \n",
    "        return str(A)+\"A\"+str(B)+\"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        if secret==guess:\n",
    "            return str(len(secret))+\"A0B\"\n",
    "        A=0\n",
    "        i=0\n",
    "        \n",
    "        while i<len(secret):\n",
    "            if secret[i]==guess[i]: \n",
    "                secret =secret[:i]+secret[i+1:]\n",
    "                guess = guess[:i]+guess[i+1:]\n",
    "                while secret[0]==guess[0]:\n",
    "                    secret =secret[i+1:]\n",
    "                    guess = guess[i+1:]\n",
    "                    A+=1\n",
    "                A+=1\n",
    "                i=0\n",
    "            i+=1\n",
    "        \n",
    "        s=Counter(secret) & Counter(guess)\n",
    "        B=sum(s.values())\n",
    "        \n",
    "        return str(A)+\"A\"+str(B)+\"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        a, b = 0, 0\n",
    "        ds = collections.defaultdict(int)\n",
    "        dg = collections.defaultdict(int)\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i] == guess[i]:\n",
    "                a += 1\n",
    "            else:\n",
    "                ds[secret[i]] += 1\n",
    "                dg[guess[i]] += 1\n",
    "        for k,v in dg.items():\n",
    "            if k in ds:\n",
    "                b += min(v, ds[k])\n",
    "        return '%dA%dB' % (a,b)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        sec = defaultdict(int)\n",
    "        gue = defaultdict(int)\n",
    "\n",
    "        same_count = 0\n",
    "        sum = 0\n",
    "\n",
    "        for i in range(0,len(guess)):\n",
    "            if secret[i] == guess[i]:\n",
    "                same_count += 1\n",
    "            else:\n",
    "                sec[secret[i]] += 1\n",
    "                gue[guess[i]] += 1\n",
    "        for item in gue:\n",
    "            sum += min(sec[item],gue[item])\n",
    "        \n",
    "        return(\"{}A{}B\".format(same_count,sum))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bull = 0\n",
    "        #cow = 0\n",
    "        m = len(secret)\n",
    "        secret = list(secret)\n",
    "        guess = list(guess)\n",
    "        #secret1 = []\n",
    "        #guess1 = []\n",
    "        for i in range(m-1,-1,-1):\n",
    "            if secret[i] == guess[i]:\n",
    "                bull += 1\n",
    "                secret.pop(i)\n",
    "                guess.pop(i)\n",
    "            #else:\n",
    "                \n",
    "            #    if j in secret1:\n",
    "            #        cow += 1\n",
    "            #        secret1.remove(j)\n",
    "\n",
    "                #secret1.append(secret[i])\n",
    "                #guess1.append(guess[i])\n",
    "        cow = 0\n",
    "        for j in guess:\n",
    "            if j in secret:\n",
    "                cow += 1\n",
    "                secret.remove(j)\n",
    "\n",
    "        #cow = len([j for j in guess1 if j in secret1])\n",
    "        return str(bull)+'A'+str(cow)+'B'\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        n=len(secret)\n",
    "        cnt1,cnt2=[0]*10,[0]*10\n",
    "        cow,bull=0,0\n",
    "        for i in range(n):\n",
    "            if secret[i]==guess[i]:\n",
    "                bull+=1\n",
    "            else:\n",
    "                cnt1[int(secret[i])]+=1\n",
    "                cnt2[int(guess[i])]+=1\n",
    "        for i in range(10):\n",
    "            cow+=min(cnt1[i],cnt2[i])\n",
    "        return str(bull)+'A'+str(cow)+'B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        A, B = 0, 0\n",
    "        dict_secret = Counter(secret)\n",
    "        dict_guess = Counter(guess)\n",
    "        for key, val in dict_secret.items():\n",
    "            if key in dict_guess.keys():\n",
    "                A += min(val, dict_guess[key])\n",
    "        for i in range(len(guess)):\n",
    "            if guess[i] == secret[i]:\n",
    "                B += 1\n",
    "        return ''.join([str(B), \"A\", str(A - B), \"B\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        a, b = 0, 0\n",
    "        mapS = defaultdict(int)\n",
    "        mapG = defaultdict(int)\n",
    "        for x, y in zip(secret, guess):\n",
    "            if x == y:\n",
    "                a += 1\n",
    "            else:\n",
    "                mapS[x] += 1\n",
    "                mapG[y] += 1\n",
    "        for num in mapS.keys():\n",
    "            b += min(mapS[num], mapG[num])\n",
    "\n",
    "        return f\"{a}A{b}B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def getHint(self, secret: str, guess: str) -> str:\r\n",
    "        bull = cow = 0\r\n",
    "        sc, gc = collections.Counter(), collections.Counter()\r\n",
    "        for i in range(len(secret)):\r\n",
    "            if secret[i] == guess[i]:\r\n",
    "                bull += 1\r\n",
    "                continue\r\n",
    "            if sc[guess[i]] > 0:\r\n",
    "                sc[guess[i]] -= 1\r\n",
    "                cow += 1\r\n",
    "            else:\r\n",
    "                gc[guess[i]] += 1\r\n",
    "            sc[secret[i]] += 1\r\n",
    "        for k, cnt in gc.items():\r\n",
    "            cow += min(sc[k], cnt)\r\n",
    "        return f'{bull}A{cow}B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        # 极简算法\n",
    "        A = sum(s==g for s,g in zip(secret,guess))\n",
    "        B = sum((collections.Counter(secret)&collections.Counter(guess)).values())-A\n",
    "        return \"{A}A{B}B\".format(A=A,B=B)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        hint = [0,'A',0,'B']\n",
    "        \n",
    "        a = 0\n",
    "        b = 0\n",
    "        \n",
    "        cnt = Counter(secret)\n",
    "        unmatch = {}\n",
    "        n = len(guess)\n",
    "        \n",
    "        for i in range(n):\n",
    "            ch = guess[i]\n",
    "            if ch == secret[i]:\n",
    "                a += 1\n",
    "                cnt[ch] -= 1\n",
    "                if cnt[ch] == 0:\n",
    "                    cnt.pop(ch)\n",
    "            elif ch in cnt:\n",
    "                unmatch[ch] = unmatch.get(ch,0) + 1\n",
    "        \n",
    "        for ch in cnt:\n",
    "            if ch in unmatch:\n",
    "                b += min(unmatch[ch], cnt[ch])\n",
    "        \n",
    "        hint[0] = str(a)\n",
    "        hint[2] = str(b)\n",
    "        \n",
    "        return \"\".join(hint)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        from collections import defaultdict\n",
    "        dic1,dic2 = defaultdict(int),defaultdict(int)\n",
    "        a,b = 0,0\n",
    "        for s,g in zip(secret,guess):\n",
    "            if s == g:\n",
    "                a += 1\n",
    "            else:\n",
    "                dic1[s] += 1\n",
    "                dic2[g] += 1\n",
    "        for k,v in dic1.items():\n",
    "            b += min(v,dic2[k])\n",
    "        return str(a) + \"A\" + str(b) + \"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 1. 解法一: 哈希表\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        secret_counter = Counter(secret)\n",
    "        guess_counter = Counter(guess)\n",
    "        m = len(secret)\n",
    "        n = len(guess)\n",
    "        i = 0\n",
    "        j = 0\n",
    "        bulls = 0\n",
    "        cows = 0\n",
    "        while i < m and j < n:\n",
    "            if secret[i] == guess[j]:\n",
    "                bulls += 1\n",
    "                secret_counter[secret[i]] -= 1\n",
    "                guess_counter[secret[i]] -= 1\n",
    "            i += 1\n",
    "            j += 1\n",
    "\n",
    "        for key in secret_counter.keys():\n",
    "            while (\n",
    "                secret_counter[key] != 0\n",
    "                and key in guess_counter\n",
    "                and guess_counter[key] != 0\n",
    "            ):\n",
    "                cows += 1\n",
    "                secret_counter[key] -= 1\n",
    "                guess_counter[key] -= 1\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "\n",
    "        mydict = {}\n",
    "        bulls = 0\n",
    "        cows = 0\n",
    "\n",
    "        for i in range(len(guess)):\n",
    "            gus = int(guess[i])\n",
    "            sec = int(secret[i])\n",
    "            if gus == sec:\n",
    "                bulls = bulls + 1\n",
    "                guess = guess[:i] +\" \"+ guess[i+1:]\n",
    "                secret = secret[:i] + \" \"+ secret[i+1:]\n",
    "\n",
    "        # print(secret)\n",
    "        # print(guess)\n",
    "\n",
    "        for c in secret:\n",
    "            if c!=\" \" and c not in mydict :\n",
    "                mydict[c] = 1\n",
    "            elif c!=\" \":\n",
    "                mydict[c] = mydict[c] + 1\n",
    "        # print(mydict)\n",
    "\n",
    "        for i in range(len(guess)):\n",
    "            if guess[i] != \" \" and guess[i] in mydict:\n",
    "                cows = cows + 1\n",
    "                mydict[guess[i]] = mydict[guess[i]] - 1\n",
    "                if mydict[guess[i]] == 0:\n",
    "                    mydict.pop(guess[i])\n",
    "\n",
    "\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        #dict_nums = dict()\n",
    "        #scollection_nums = collections.Counter(secret)\n",
    "        #print(collection_nums)\n",
    "        sec_list = list(secret)\n",
    "        #list(secret)\n",
    "        gus_list = list(guess)\n",
    "        #list(guess)\n",
    "        collection_sec = collections.Counter(sec_list)\n",
    "        collection_gus = collections.Counter(gus_list)\n",
    "        count = 0\n",
    "        countA = 0\n",
    "        for i in collection_gus:\n",
    "            #print(collection_gus)\n",
    "            #print(collection_sec)\n",
    "            #print(i)\n",
    "            if i in collection_sec:\n",
    "\n",
    "                count += min(collection_gus[i], collection_sec[i])\n",
    "        for i in range(0, len(sec_list)):\n",
    "            if sec_list[i] == gus_list[i]:\n",
    "                countA += 1\n",
    "        res = str(countA) + 'A' + str(count-countA) + 'B'\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        x = []\n",
    "        a = {}\n",
    "        b = {}\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i] == guess[i]:\n",
    "                x.append(i)\n",
    "            else:\n",
    "                if secret[i] not in a:\n",
    "                    a[secret[i]] = 0\n",
    "                a[secret[i]] += 1\n",
    "                if guess[i] not in b:\n",
    "                    b[guess[i]] = 0\n",
    "                b[guess[i]] += 1\n",
    "        po = 0\n",
    "        for i in b:\n",
    "            if i in a:\n",
    "                po+=min(b[i],a[i])\n",
    "        return str(len(x))+\"A\"+str(po)+\"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls = 0\n",
    "        cntS, cntG = [0] * 10, [0] * 10\n",
    "        for s, g in zip(secret, guess):\n",
    "            if s == g:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cntS[int(s)] += 1\n",
    "                cntG[int(g)] += 1\n",
    "        cows = sum(min(s, g) for s, g in zip(cntS, cntG))\n",
    "        return f'{bulls}A{cows}B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bull = 0\n",
    "        cnt = defaultdict(int)\n",
    "        \n",
    "        for i in range(len(guess)):\n",
    "            if secret[i] == guess[i]:\n",
    "                bull += 1\n",
    "            else:\n",
    "                cnt[secret[i]] += 1\n",
    "\n",
    "        cow = 0\n",
    "\n",
    "        for i in range(len(guess)):\n",
    "            c = guess[i]\n",
    "            if c in cnt and secret[i] != guess[i]:\n",
    "                cnt[c] -= 1\n",
    "                cow += 1\n",
    "                if cnt[c] == 0:\n",
    "                    cnt.pop(c)\n",
    "                \n",
    "        return f'{bull}A{cow}B'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        b_num = 0\n",
    "        c_num = 0\n",
    "        hmap_1 = Counter(secret)\n",
    "        hmap_2 = Counter(guess)\n",
    "        for key, value in hmap_1.items():\n",
    "            c_num += min(value, hmap_2[key])\n",
    "        \n",
    "        for c1, c2 in zip(secret, guess):\n",
    "            if c1 == c2:\n",
    "                b_num += 1\n",
    "        \n",
    "        return str(b_num) + 'A' + str(c_num-b_num) + 'B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        num_len=len(secret)\n",
    "        s_count=[0 for _ in range(10)]\n",
    "        g_count=[0 for _ in range(10)]\n",
    "        bulls_num=0\n",
    "        for index in range(num_len):\n",
    "            if secret[index]==guess[index]:\n",
    "                bulls_num+=1\n",
    "            else:\n",
    "                s_count[int(secret[index])]+=1\n",
    "                g_count[int(guess[index])]+=1\n",
    "        cows_num=0\n",
    "        for index in range(10):\n",
    "            one=min(s_count[index],g_count[index])\n",
    "            cows_num+=one\n",
    "        return f'{bulls_num}A{cows_num}B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls, cnts_s, cnts_g = 0, defaultdict(int), defaultdict(int)\n",
    "        for i, c in enumerate(secret):\n",
    "            if guess[i] == c:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cnts_s[c] += 1\n",
    "                cnts_g[guess[i]] += 1\n",
    "        cows = 0\n",
    "        for k in cnts_s.keys():\n",
    "            cows += min(cnts_s[k], cnts_g[k])\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        a,b = 0,0\n",
    "        rs, rg = defaultdict(int), defaultdict(int)\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i] == guess[i]:\n",
    "                a += 1\n",
    "            else:\n",
    "                rs[secret[i]] += 1\n",
    "                rg[guess[i]] += 1\n",
    "        for k,v in rg.items():\n",
    "            b += min(rs[k], v)\n",
    "        return f\"{a}A{b}B\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        secretMap = collections.defaultdict(int)\n",
    "        for s in secret:\n",
    "            secretMap[s] += 1\n",
    "\n",
    "        bulls = 0\n",
    "        cows = 0\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i] == guess[i]:\n",
    "                bulls += 1\n",
    "                secretMap[secret[i]] -= 1\n",
    "                if secretMap[secret[i]] < 0:\n",
    "                    cows -= 1\n",
    "                continue\n",
    "\n",
    "            if guess[i] in secretMap and secretMap[guess[i]] > 0:\n",
    "                cows += 1\n",
    "                secretMap[guess[i]] -= 1\n",
    "\n",
    "        return f\"{bulls}A{cows}B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        n = len(secret)\n",
    "        h1 = [0] * 10\n",
    "        h2 = [0] * 10\n",
    "        bullCnt, cowCnt = 0, 0\n",
    "        for i in range(n):\n",
    "            if secret[i] == guess[i]: bullCnt += 1\n",
    "            else:\n",
    "                h1[ord(secret[i]) - ord('0')] += 1\n",
    "                h2[ord(guess[i]) - ord('0')] += 1\n",
    "        for i in range(10):\n",
    "            cowCnt += min(h1[i], h2[i])\n",
    "        return str(bullCnt) + \"A\" + str(cowCnt) + \"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls, cnts_s, cnts_g = 0, defaultdict(int), defaultdict(int)\n",
    "        for i, c in enumerate(secret):\n",
    "            if guess[i] == c:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cnts_s[c] += 1\n",
    "                cnts_g[guess[i]] += 1\n",
    "        cows = 0\n",
    "        for k in cnts_s.keys():\n",
    "            cows += min(cnts_s[k], cnts_g[k])\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        if secret==guess:\n",
    "            return str(len(secret))+\"A0B\"\n",
    "        A=0\n",
    "        i=0\n",
    "        \n",
    "        while i<len(secret):\n",
    "            if secret[i]==guess[i]: \n",
    "                secret =secret[:i]+secret[i+1:]\n",
    "                guess = guess[:i]+guess[i+1:]\n",
    "                while secret[0]==guess[0]:\n",
    "                    secret =secret[1:]\n",
    "                    guess = guess[1:]\n",
    "                    A+=1\n",
    "                A+=1\n",
    "                i=0\n",
    "            i+=1\n",
    "        \n",
    "        s=Counter(secret) & Counter(guess)\n",
    "        B=sum(s.values())\n",
    "        \n",
    "        return f\"{A}A{B}B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        a, b = 0, 0\n",
    "        sl, gl = [0]*10 , [0]*10\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i]==guess[i]:\n",
    "                a += 1\n",
    "                continue\n",
    "            sl[int(secret[i])-0] +=1\n",
    "            gl[int(guess[i])-0] +=1\n",
    "        for i in range(10):\n",
    "            b = b + min(sl[i], gl[i])\n",
    "        return str(a)+'A'+str(b)+'B'\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        m = dict()\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i] == guess[i]:\n",
    "                a += 1\n",
    "            else:\n",
    "                m[secret[i]] = m.get(secret[i], 0) + 1\n",
    "                if m[secret[i]] - 1 < 0:\n",
    "                    b += 1\n",
    "                m[guess[i]] = m.get(guess[i], 0) - 1\n",
    "                if m[guess[i]] + 1 > 0:\n",
    "                    b += 1\n",
    "        return str(a) + 'A' + str(b) + 'B'\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        A=sum(s==g for s,g in zip(secret,guess))\n",
    "        B=sum((collections.Counter(secret)&collections.Counter(guess)).values())-A\n",
    "        return \"{A}A{B}B\".format(A=A,B=B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        return \"{}A{}B\".format((bulls:=sum(secret[i] == guess[i] for i in range(len(secret)))), sum((Counter(secret) & Counter(guess)).values()) - bulls)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls = 0\n",
    "        cntS, cntG = [0] * 10, [0] * 10\n",
    "\n",
    "        for s, g in zip(secret, guess):\n",
    "            if s == g:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cntS[int(s)] += 1\n",
    "                cntG[int(g)] += 1\n",
    "        cows = sum(min(s, g) for s, g in zip(cntS, cntG))\n",
    "        return f'{bulls}A{cows}B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        if secret==guess:\n",
    "            return str(len(secret))+\"A0B\"\n",
    "        A=0\n",
    "        i=0\n",
    "        sl = []\n",
    "        gl = []\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i] == guess[i]:\n",
    "                A += 1\n",
    "            else:\n",
    "                sl.append(secret[i])\n",
    "                gl.append(guess[i])\n",
    "        \n",
    "        s=Counter(sl) & Counter(gl)\n",
    "        B=sum(s.values())\n",
    "        \n",
    "        return f\"{A}A{B}B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        A = B = 0\n",
    "        nums = [0] * 10\n",
    "        for i in range(len(guess)):\n",
    "            if secret[i] == guess[i]:\n",
    "                A += 1\n",
    "            else:\n",
    "                if nums[int(secret[i])] < 0:\n",
    "                    B += 1\n",
    "                nums[int(secret[i])] += 1\n",
    "                if nums[int(guess[i])] > 0:\n",
    "                    B += 1\n",
    "                nums[int(guess[i])] -= 1\n",
    "        return F\"{A}A{B}B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        #dict_nums = dict()\n",
    "        #scollection_nums = collections.Counter(secret)\n",
    "        #print(collection_nums)\n",
    "        sec_list = list(secret)\n",
    "        #list(secret)\n",
    "        gus_list = list(guess)\n",
    "        #list(guess)\n",
    "        collection_sec = collections.Counter(sec_list)\n",
    "        collection_gus = collections.Counter(gus_list)\n",
    "        count = 0\n",
    "        countA = 0\n",
    "        for i in collection_gus:\n",
    "            print(collection_gus)\n",
    "            print(collection_sec)\n",
    "            print(i)\n",
    "            if i in collection_sec:\n",
    "\n",
    "                count += min(collection_gus[i], collection_sec[i])\n",
    "        for i in range(0, len(sec_list)):\n",
    "            if sec_list[i] == gus_list[i]:\n",
    "                countA += 1\n",
    "        res = str(countA) + 'A' + str(count-countA) + 'B'\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        \n",
    "        \n",
    "        bulls = 0\n",
    "        cntS, cntG = [0] * 10, [0] * 10\n",
    "        for s, g in zip(secret, guess):\n",
    "            if s == g:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cntS[int(s)] += 1\n",
    "                cntG[int(g)] += 1\n",
    "        cows = sum(min(s, g) for s, g in zip(cntS, cntG))\n",
    "        return f'{bulls}A{cows}B'\n",
    "\n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        cnt = Counter(secret)\n",
    "        A, B = 0, 0\n",
    "        for i, num in enumerate(guess):\n",
    "            if cnt[num] > 0:\n",
    "                B += 1\n",
    "                cnt[num] -= 1\n",
    "            if guess[i] == secret[i]:\n",
    "                A += 1\n",
    "        B -= A\n",
    "        return str(A) + 'A' + str(B) + 'B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        l1 = []\n",
    "        l2 = []\n",
    "        count = 0\n",
    "\n",
    "        for i,each in enumerate(secret):\n",
    "            if each == guess[i]:\n",
    "                count+=1\n",
    "            else:\n",
    "                l1.append(each)\n",
    "                l2.append(guess[i])\n",
    "        for i in l1:\n",
    "            if i in l2:\n",
    "                l2.remove(i)\n",
    "        count1 = len(secret)-count-len(l2)\n",
    "\n",
    "        return str(count)+'A'+str(count1)+'B'\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def getHint(self, secret: str, guess: str) -> str:\r\n",
    "        n = len(secret)\r\n",
    "        bulls = 0\r\n",
    "        cows_bulls = 0\r\n",
    "        count1, count2 = collections.Counter(secret), collections.Counter(guess)\r\n",
    "        for i in range(n):\r\n",
    "            if secret[i] == guess[i]:\r\n",
    "                bulls += 1\r\n",
    "        for key, value in count1.items():\r\n",
    "            cows_bulls += min(value, count2.get(key, 0))\r\n",
    "        return str(bulls) + \"A\" + str(cows_bulls - bulls) + \"B\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bull, cow = 0,0\n",
    "        s = list(secret)\n",
    "        g = list(guess)\n",
    "\n",
    "        i,j=0,0\n",
    "        while i<len(secret):\n",
    "            if s[j] == g[j]:\n",
    "                s.pop(j)\n",
    "                g.pop(j)\n",
    "                bull+=1\n",
    "            else:\n",
    "                j+=1\n",
    "            i+=1\n",
    "\n",
    "        c = Counter(s)\n",
    "        for l in g:\n",
    "            if l in c and c[l]>0:\n",
    "                cow +=1\n",
    "                c[l]-=1\n",
    "        return str(bull)+\"A\"+str(cow)+\"B\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls = 0\n",
    "        cntS, cntG = [0] * 10, [0] * 10\n",
    "        for s, g in zip(secret, guess):\n",
    "            if s == g:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cntS[int(s)] += 1\n",
    "                cntG[int(g)] += 1\n",
    "        cows = sum(min(s, g) for s, g in zip(cntS, cntG))\n",
    "        return f'{bulls}A{cows}B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        sec_list = list(secret)\n",
    "        gus_list = list(guess)\n",
    "        collection_sec = collections.Counter(sec_list)\n",
    "        collection_gus = collections.Counter(gus_list)\n",
    "        count = 0\n",
    "        countA = 0\n",
    "        for i in collection_gus:\n",
    "            if i in collection_sec:\n",
    "\n",
    "                count += min(collection_gus[i], collection_sec[i])\n",
    "        for i in range(0, len(sec_list)):\n",
    "            if sec_list[i] == gus_list[i]:\n",
    "                countA += 1\n",
    "        res = str(countA) + 'A' + str(count-countA) + 'B'\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        secret_list = list(secret)\n",
    "        guess_list = list(guess)\n",
    "        A = [guess_list[i] for i in range(len(guess_list)) if guess_list[i] == secret_list[i]]\n",
    "        new_list_B = [guess_list[i] for i in range(len(guess_list)) if guess_list[i] != secret_list[i]]\n",
    "        new_list_A = [secret_list[i] for i in range(len(guess_list)) if guess_list[i] != secret_list[i]]\n",
    "        B = 0\n",
    "        for e in new_list_B:\n",
    "            if e in new_list_A:\n",
    "                new_list_A.remove(e)\n",
    "                B += 1\n",
    "\n",
    "        result = str(len(A)) + \"A\" + str(B) + \"B\"\n",
    "        print(result)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        bulls, cnts_s, cnts_g = 0, defaultdict(int), defaultdict(int)\n",
    "        for i, c in enumerate(secret):\n",
    "            if guess[i] == c:\n",
    "                bulls += 1\n",
    "            else:\n",
    "                cnts_s[c] += 1\n",
    "                cnts_g[guess[i]] += 1\n",
    "        cows = 0\n",
    "        for k in cnts_s.keys():\n",
    "            cows += min(cnts_s[k], cnts_g[k])\n",
    "        return str(bulls) + \"A\" + str(cows) + \"B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        A,B = 0,0\n",
    "        for i in range(len(secret)):\n",
    "            if secret[i] == guess[i]:\n",
    "                A += 1\n",
    "        cnt1 = Counter(secret)\n",
    "        cnt2 = Counter(guess)\n",
    "        for i in cnt1:\n",
    "            if i in cnt2:\n",
    "                B += min(cnt1[i],cnt2[i])\n",
    "        B -= A\n",
    "        return ''.join([str(A),'A',str(B),'B'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        if secret==guess:\n",
    "            return str(len(secret))+\"A0B\"\n",
    "        A=0\n",
    "        i=0\n",
    "        \n",
    "        while i<len(secret):\n",
    "            if secret[i]==guess[i]: \n",
    "                secret =secret[:i]+secret[i+1:]\n",
    "                print(secret)\n",
    "                guess = guess[:i]+guess[i+1:]\n",
    "                while secret[0]==guess[0]:\n",
    "                    secret =secret[i+1:]\n",
    "                    guess = guess[i+1:]\n",
    "                    A+=1\n",
    "                A+=1\n",
    "                i=0\n",
    "            i+=1\n",
    "        \n",
    "        s=Counter(secret) & Counter(guess)\n",
    "        B=sum(s.values())\n",
    "        \n",
    "        return str(A)+\"A\"+str(B)+\"B\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getHint(self, secret: str, guess: str) -> str:\n",
    "        Bull = 0\n",
    "        candidateSecret = {}\n",
    "        candidateGuess = {}\n",
    "        for i in range(min(len(secret),len(guess))):\n",
    "            if secret[i] == guess[i]:\n",
    "                Bull += 1\n",
    "            else:\n",
    "                candidateSecret[secret[i]] = candidateSecret.get(secret[i],0)+1\n",
    "                candidateGuess[guess[i]] = candidateGuess.get(guess[i],0)+1\n",
    "\n",
    "        # for i in range(min(len(secret), len(guess)), len(secret)):\n",
    "        #     if secret[i] in candidateSecret:\n",
    "        #         candidateSecret[secret[i]] += 1\n",
    "        #     else:\n",
    "        #         candidateSecret[secret[i]] = 1\n",
    "        \n",
    "        # for i in range(min(len(secret), len(guess)), len(guess)):\n",
    "        #     if guess[i] in candidateGuess:\n",
    "        #         candidateGuess[guess[i]] += 1\n",
    "        #     else:\n",
    "        #         candidateGuess[guess[i]] = 1\n",
    "\n",
    "        Cows = 0\n",
    "        for key in candidateSecret:\n",
    "            Cows += min(candidateSecret[key], candidateGuess.get(key,0))\n",
    "\n",
    "        return str(Bull)+\"A\"+str(Cows) + \"B\"\n",
    "            \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
